﻿using System;
using System.Collections.Generic;
using Config;
using DAL;
using Security;
using System.Data;

namespace Accounting
{
    public class Module
    {
        public static readonly string ID = "Module Accounting";
        public static readonly string Description = "Creating And Processing Accounting Data";
        public static readonly string DBConnectionsStringKey = "accountingDBConnectionString";

        DataBridge mDataBridge = null;

        public static void Register()
        {
            Registrar.RegisterModule(ID, Description);
            Domains.Administration.Register();
            Domains.Calculation.Register();
            Domains.InputData.Register();
            Domains.Reports.Register();
        }

        private string mLastError = string.Empty;
        private string Plus = string.Empty;

        public string LastError
        {
            get
            {
                return mLastError;
            }
        }
        
        public Module()
        {
            mDataBridge = new DataBridge(ConfigManager.GetDbConnectionString(Module.DBConnectionsStringKey), ConfigManager.GetProviderName(Module.DBConnectionsStringKey));
            Plus = mDataBridge.ConcatSimbol + " ' ' " + mDataBridge.ConcatSimbol;
        }


        #region Bank Region

        public DataTable GetBankById(string bankID)
        {
            DataTable resultDataTable = new DataTable();

            try
            {                
                string commandText = @"SELECT * "
                                        + " FROM Bank "
                                        + " WHERE bankID = " + bankID;

                resultDataTable = mDataBridge.ExecuteQuery(commandText); // PG compliant
                mLastError = mDataBridge.LastError;                
            }
            catch (Exception exception)
            {
                mLastError += "Error using DataBridge. " + exception.Message;
            }

            return resultDataTable;
        }

        public DataTable GetBankList()
        {
            DataTable resultDataTable = new DataTable();

            try
            {
                string commandText = @"SELECT * "
                                     + " FROM Bank "
                                     + " ORDER BY Name ";

                resultDataTable = mDataBridge.ExecuteQuery(commandText); // PG compliant
                mLastError = mDataBridge.LastError;
            }
            catch (Exception exception)
            {
                mLastError += "Error using DataBridge. " + exception.Message;
            }

            return resultDataTable;
        }

        public bool AddBank(string name, string iban, string correspondentAccount)
        {
            bool result = false;
            try
            {                
                string nonQuery = @"INSERT INTO Bank (Name, IBAN, CorespondentAccount)"
                                        + " VALUES ( '" + name + "', '" + iban + "', '" + correspondentAccount + "')";

                result = mDataBridge.ExecuteNonQuery(nonQuery); // PG compliant
                mLastError = mDataBridge.LastError;
            }
            catch (Exception exception)
            {
                mLastError += "Error using DataBridge. " + exception.Message;
            }

            return result;
        }

        public bool UpdateBank(int bankID, string name, string iban, string correspondentAccount)
        {
            bool result = false;
            try
            {
                string nonQuery = @"Update Bank "
                       + " SET Name = '" + name + "' "
                       + ", IBAN = '" + iban + "' "
                       + ", CorespondentAccount = '" + correspondentAccount + "' "
                       + " WHERE bankID = " + bankID;

                result = mDataBridge.ExecuteNonQuery(nonQuery); // PG compliant
                mLastError = mDataBridge.LastError;
            }
            catch (Exception exception)
            {
                mLastError += "Error using DataBridge. " + exception.Message;
            }

            return result;
        }

        public bool DeleteBank(int bankID)
        {
            bool result = false;
            try
            {               
                string nonQuery = @"Delete From Bank WHERE bankID = " + bankID;

                result = mDataBridge.ExecuteNonQuery(nonQuery); // PG compliant
                mLastError = mDataBridge.LastError;
            }
            catch (Exception exception)
            {
                mLastError += "Error using DataBridge. " + exception.Message;
            }

            return result;
        }

        public string GetBankIBAN_ByBankID(int bankID)
        {
            string result = string.Empty;

            try
            {
                string commandText = @"SELECT IBAN "
                                     + " FROM Bank "
                                     + " WHERE BankID =  " + bankID;

                DataTable resultDataTable = mDataBridge.ExecuteQuery(commandText); // PG compliant
                mLastError = mDataBridge.LastError;

                if (resultDataTable != null && resultDataTable.Rows.Count == 1)
                {
                    result = resultDataTable.Rows[0][0].ToString();
                }
            }
            catch (Exception exception)
            {
                mLastError += "Error using DataBridge. " + exception.Message;
            }

            return result;
        }

        #endregion Bank Region

        #region PlanCont Region

        public DataTable GetPlanContList()
        {
            DataTable result = new DataTable();

            try
            {
                string commandText = @"SELECT * FROM PlanCont ";

                result = mDataBridge.ExecuteQuery(commandText); // PG compliant
                mLastError = mDataBridge.LastError;               
            }
            catch (Exception exception)
            {
                mLastError += "Error using DataBridge. " + exception.Message;
            }

            return result;
        }

        #endregion PlanCont Region

    }

    namespace Domains
    {
        /// <summary>
        /// Default domain. Administration.
        /// </summary>
        public class Administration
        {
            public static readonly string Name = "Administration";
            public static readonly string Description = "Administration";

            public static void Register()
            {
                Registrar.RegisterDomain(Module.ID, Name, Description);
            }
        }

        /// <summary>
        /// Default domain. Input Data.
        /// </summary>
        public class InputData
        {
            public static readonly string Name = "Input Data";
            public static readonly string Description = "Input Data";

            public static void Register()
            {
                Registrar.RegisterDomain(Module.ID, Name, Description);
            }
        }

        /// <summary>
        /// Default domain. Calculations.
        /// </summary>
        public class Calculation
        {
            public static readonly string Name = "Calculation";
            public static readonly string Description = "Calculation";

            public static void Register()
            {
                Registrar.RegisterDomain(Module.ID, Name, Description);
            }
        }

        /// <summary>
        /// Reports
        /// </summary>
        public class Reports
        {
            public static readonly string Name = "Reports";
            public static readonly string Description = "Reports";

            public static void Register()
            {
                Registrar.RegisterDomain(Module.ID, Name, Description);
            }
        }
    }
}
